package com.jstyle.jclife.activity;

import android.Manifest;
import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.jstyle.blesdk.cmdenum.SendCmdState;
import com.jstyle.blesdk.constant.DeviceKey;
import com.jstyle.blesdk.model.DeviceBaseParameter;
import com.jstyle.blesdk.model.SetPersonalInfo;
import com.jstyle.jclife.NetWork.NetWorkUtil;
import com.jstyle.jclife.R;
import com.jstyle.jclife.app.MyApplication;
import com.jstyle.jclife.daoManager.UserInfoDaoManager;
import com.jstyle.jclife.model.EnableData;

import com.jstyle.jclife.model.JstyleDevice;
import com.jstyle.jclife.model.UserInfo;
import com.jstyle.jclife.utils.GlobalConst;
import com.jstyle.jclife.utils.PermissionsUtil;
import com.jstyle.jclife.utils.ScreenUtils;
import com.jstyle.jclife.utils.SharedPreferenceUtils;
import com.jstyle.jclife.utils.Utils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import no.nordicsemi.android.dfu.internal.manifest.ManifestFile;

/**
 * 手环设置
 */
public class DeviceSettingActivity extends BaseActivity {


    public static final String KEY_Device = "Device";
    public static final String KEY_Unit = "Unit";
    public static final String Enable_Type = "Enable_Typepublic ";
    List<EnableData> enableDataList = new ArrayList<>();
    @BindView(R.id.bt_goal_back)
    Button btGoalBack;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_share)
    ImageView ivShare;
    @BindView(R.id.text_item_other)
    TextView textItemOther;
    @BindView(R.id.RadioButton_km)
    RadioButton RadioButtonKm;
    @BindView(R.id.RadioButton_mile)
    RadioButton RadioButtonMile;
    @BindView(R.id.RadioGroup_distance)
    RadioGroup RadioGroupDistance;
    @BindView(R.id.RadioButton_12)
    RadioButton RadioButton12;
    @BindView(R.id.RadioButton_24)
    RadioButton RadioButton24;
    @BindView(R.id.RadioGroup_timemode)
    RadioGroup RadioGroupTimemode;
    @BindView(R.id.bt_to_clock)
    Button btToClock;
    @BindView(R.id.bt_to_notify)
    Button btToNotify;

    @BindView(R.id.RadioButton_brightness0)
    RadioButton RadioButtonBrightness0;
    @BindView(R.id.RadioButton_brightness1)
    RadioButton RadioButtonBrightness1;
    @BindView(R.id.RadioButton_brightness2)
    RadioButton RadioButtonBrightness2;
    @BindView(R.id.RadioGroup_brightness)
    RadioGroup RadioGroupBrightness;
    @BindView(R.id.SwitchCompat_Lift_the_wrist)
    SwitchCompat SwitchCompatLiftTheWrist;
    @BindView(R.id.RadioButton_left)
    RadioButton RadioButtonLeft;
    @BindView(R.id.RadioButton_right)
    RadioButton RadioButtonRight;
    @BindView(R.id.RadioGroup_hand)
    RadioGroup RadioGroupHand;
    @BindView(R.id.SwitchCompat_findPhone)
    SwitchCompat SwitchCompatFindPhone;
    @BindView(R.id.bt_toCamera)
    Button btToCamera;
    @BindView(R.id.ll_brightness)
    LinearLayout llBrightness;
    @BindView(R.id.ll_wrist)
    LinearLayout llWrist;
    @BindView(R.id.tv_serchdevice)
    TextView tvSerchdevice;
    @BindView(R.id.ll_findphone)
    LinearLayout llFindphone;
    private boolean enable_Chronograph;
    private boolean enable_Camera;
    private boolean enable_Lift_the_wrist;
    private boolean enable_DistanceUnit;
    private boolean enable_TimeMode;
    private static final String TAG = "DeviceSettingActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enable);
        ButterKnife.bind(this);
        initData();
        init();
    }

    private void initData() {
        enable_DistanceUnit = SharedPreferenceUtils.getBoolean(GlobalConst.KEY_DistanceUnit, false);
        enable_TimeMode = SharedPreferenceUtils.getBoolean(GlobalConst.KEY_TimeMode, true);
    }


    private void init() {
        llBrightness.setVisibility(MyApplication.getJstyleDevice()
                .getDeviceNo().equals(JstyleDevice.device_1755) ? View.VISIBLE : View.INVISIBLE);

        llFindphone.setVisibility(MyApplication.getJstyleDevice()
                .getDeviceNo().equals(JstyleDevice.device_1810) ? View.GONE : View.VISIBLE);
        tvSerchdevice.setVisibility(MyApplication.getJstyleDevice()
                .getDeviceNo().equals(JstyleDevice.device_1810) ? View.GONE : View.VISIBLE);
        ivShare.setVisibility(View.GONE);
        btGoalBack.setText(R.string.Device_Setting);
        ScreenUtils.setTitleTypeface(btGoalBack);

        RadioGroupTimemode.check(enable_TimeMode ? R.id.RadioButton_24 : R.id.RadioButton_12);
        RadioGroupDistance.check(enable_DistanceUnit ? R.id.RadioButton_mile : R.id.RadioButton_km);
        RadioGroupTimemode.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                boolean enable = false;
                switch (checkedId) {
                    case R.id.RadioButton_12:
                        enable = false;
                        break;
                    case R.id.RadioButton_24:
                        enable = true;
                        break;
                }
                SharedPreferenceUtils.setSpBoolean(GlobalConst.KEY_TimeMode, enable);
                setDeviceInfo();
            }
        });
        RadioGroupDistance.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                boolean enable = false;
                switch (checkedId) {
                    case R.id.RadioButton_km:
                        enable = false;
                        break;
                    case R.id.RadioButton_mile:
                        enable = true;
                        break;
                }
                SharedPreferenceUtils.setSpBoolean(GlobalConst.KEY_DistanceUnit, enable);
                setDeviceInfo();
            }
        });
        RadioGroupHand.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                setDeviceInfo();
            }
        });
        RadioGroupBrightness.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                setDeviceInfo();
            }
        });
        registerSwitchListener(SwitchCompatFindPhone, GlobalConst.KEY_Find_Phone, true);
        registerSwitchListener(SwitchCompatLiftTheWrist, GlobalConst.KEY_Lift_the_wrist, false);
        sendData(SendCmdState.GET_DEVICE_INFO);
    }

    private int getBrightnessLevel() {
        int brightness = 0;
        switch (RadioGroupBrightness.getCheckedRadioButtonId()) {
            case R.id.RadioButton_brightness0:
                brightness = 0;
                break;
            case R.id.RadioButton_brightness1:
                brightness = 9;
                break;
            case R.id.RadioButton_brightness2:
                brightness = 15;
                break;
        }
        return brightness;
    }


    private void setDeviceInfo() {
        if (isReceiver) return;
        DeviceBaseParameter deviceBaseParameter = new DeviceBaseParameter();
        deviceBaseParameter.setDistanceUnit(RadioGroupDistance.getCheckedRadioButtonId() == R.id.RadioButton_mile);
        deviceBaseParameter.setHandleState(RadioGroupHand.getCheckedRadioButtonId() == R.id.RadioButton_left);
        deviceBaseParameter.setRainHandEnable(SwitchCompatLiftTheWrist.isChecked());
        deviceBaseParameter.setBrightnessLevel(getBrightnessLevel());

        //  deviceBaseParameter.setScreenState(radioGroupDisplay.getCheckedRadioButtonId()==R.id.radio_display_h);
        deviceBaseParameter.setShowHour(RadioGroupTimemode.getCheckedRadioButtonId() == R.id.RadioButton_12);
        sendData(SendCmdState.SET_DEVICE_INFO, deviceBaseParameter);
    }

    private void registerSwitchListener(SwitchCompat switchCompat, final String key, boolean defaultEnable) {
        boolean enable = SharedPreferenceUtils.getBoolean(key, defaultEnable);
        ScreenUtils.setSwitchColor(switchCompat);
        switchCompat.setChecked(enable);
        switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferenceUtils.setSpBoolean(key, isChecked);
                if (key.equals(GlobalConst.KEY_Lift_the_wrist)) setDeviceInfo();
            }
        });
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }

    boolean enable;

    @OnClick({R.id.bt_to_clock, R.id.bt_to_notify, R.id.bt_toCamera})
    public void onViewClicked(View view) {
        if(Utils.isFastClick()){return;}
        switch (view.getId()) {
            case R.id.bt_toCamera:
                PermissionsUtil.requestPermissions(this, new PermissionsUtil.PermissionListener() {
                    @Override
                    public void granted(String name) {
                        startActivity(CameraActivity.class);
                    }

                    @Override
                    public void NeverAskAgain() {

                    }

                    @Override
                    public void disallow(String name) {

                    }
                }, Manifest.permission.CAMERA);

                break;
            case R.id.bt_to_clock:
                startActivity(ClockActivity.class);
                break;
            case R.id.bt_to_notify:
                startActivity(NotifySettingActivity.class);
                break;
        }
    }

    private boolean isReceiver;

    @Override
    public void dataCallback(Map<String, String> maps, SendCmdState sendCmdState) {
        super.dataCallback(maps, sendCmdState);
        switch (sendCmdState) {
            case GET_DEVICE_INFO:
                isReceiver = true;
                int timeMode = Integer.parseInt(maps.get(DeviceKey.KHourState));
                int distanceUnit = Integer.parseInt(maps.get(DeviceKey.KDistanceUnit));
                int handEnable = Integer.parseInt(maps.get(DeviceKey.KHandleEnable));
                int handStatus = Integer.parseInt(maps.get(DeviceKey.KHandleSign));
                int brightnessLevel = Integer.parseInt(maps.get(DeviceKey.KBrightnessLevel));
                setBrightnessLevelStatus(brightnessLevel);
                RadioGroupTimemode.check(timeMode == 1 ? R.id.RadioButton_12 : R.id.RadioButton_24);
                RadioGroupDistance.check(distanceUnit == 1 ? R.id.RadioButton_mile : R.id.RadioButton_km);
                RadioGroupHand.check(handStatus == 1 ? R.id.RadioButton_left : R.id.RadioButton_right);
                SwitchCompatLiftTheWrist.setChecked(handEnable == 1);
                isReceiver = false;
                break;
        }
    }

    private void setBrightnessLevelStatus(int level) {
        switch (level) {
            case 0:
                RadioGroupBrightness.check(R.id.RadioButton_brightness0);
                break;
            case 9:
                RadioGroupBrightness.check(R.id.RadioButton_brightness1);
                break;
            case 15:
                RadioGroupBrightness.check(R.id.RadioButton_brightness2);
                break;
        }
    }

}
